/*	talmud - The TALMUD roguelike
 * Copyright (C) 2012, talmud contributors
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of Version 2 of the GNU General Public License as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE. See Version 2 of the GNU General Public
 *  License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "item.h"
#include "race.h"
#include "itemmanage.h"
#include "talmud.h"
#include "rand.h"
#include "draw.h"

const char * const SLOTNAMES[] = {
	"strange piece of armor",
	"body armor",
	"helm",
	"boots",
	"gloves",
	"ring",
	"cloak",
};

const itemdef_t ITEMDEFS[ID_COUNT] = {
	// -- Weapons --
	{	// 0
		"dagger", I_WEAPON, UNID_NONE,
		S_NONE, 0, 3, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 4, 0 },
		U_NONE, 15,
		F_NORMAL,
		3,
		A_CYAN,
	},

	{	// 1
		"elven dagger", I_WEAPON, UNID_NONE,
		S_NONE, 0, 2, I_NONE,
		1, 1,
		{ 1, 0 },
		{ 5, 0 },
		U_NONE, 15,
		F_UNUSUAL,
		3,
		A_CYAN,
	},

	{	// 2
		"curved dagger", I_WEAPON, UNID_NONE,
		S_NONE, 0, 3, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 3, 0 },
		U_NONE, 15,
		F_NORMAL,
		3,
		A_CYAN,
	},

	{	// 3
		"silver dagger", I_WEAPON, UNID_NONE,
		S_NONE, 0, 3, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 4, 0 },
		U_NONE, 15,
		F_NORMAL,
		3,
		A_CYAN,
	},

	{	// 4
		"short sword", I_WEAPON, UNID_NONE,
		S_NONE, 0, 7, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 6, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{	// 5
		"curved sword", I_WEAPON, UNID_NONE,
		S_NONE, 0, 6, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 6, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{	// 6
		"notched sword", I_WEAPON, UNID_NONE,
		S_NONE, 0, 7, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 7, 0 },
		U_NONE, 1,
		F_UNUSUAL,
		1,
		A_CYAN,
	},

	{	// 7
		"bastard sword", I_WEAPON, UNID_NONE,
		S_NONE, 0, 8, I_NONE,
		1, 1,
		{ 1, 0 },
		{ 8, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{	// 8
		"long sword", I_WEAPON, UNID_NONE,
		S_NONE, 0, 10, I_NONE,
		0, 1,
		{ 2, 0 },
		{ 4, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{	// 9
		"claymore", I_WEAPON, UNID_NONE,
		S_NONE, 0, 15, I_NONE,
		0, 1,
		{ 2, 0 },
		{ 8, 0 },
		U_NONE, 1,
		F_RARE,
		1,
		A_CYAN,
	},

	{	// 10
		"mace", I_WEAPON, UNID_NONE,
		S_NONE, 0, 8, I_NONE,
		1, 1,
		{ 1, 0 },
		{ 8, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{	// 11
		"club", I_WEAPON, UNID_NONE,
		S_NONE, 0, 5, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 6, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_YELLOW,
	},

	{	// 12
		"wooden stake", I_WEAPON, UNID_NONE,
		S_NONE, 0, 1, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 4, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_YELLOW,
	},

	{	// 13
		"sicle", I_WEAPON, UNID_NONE,
		S_NONE, 0, 4, I_NONE,
		0, 1,
		{ 1, 0 },
		{ 4, 0 },
		U_NONE, 1,
		F_UNUSUAL,
		1,
		A_CYAN,
	},

	// -- Armor --
	{
		"leather armor", I_ARMOR, UNID_NONE,
		S_BODY, 2, 25, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_YELLOW,
	},

	{
		"buckled leather suit", I_ARMOR, UNID_NONE,
		S_BODY, 3, 35, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_YELLOW,
	},

	{
		"elven leather armor", I_ARMOR, UNID_NONE,
		S_BODY, 3, 20, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_YELLOW,
	},

	{
		"ring mail suit", I_ARMOR, UNID_NONE,
		S_BODY, 4, 50, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	{
		"chain mail suit", I_ARMOR, UNID_NONE,
		S_BODY, 5, 75, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	// -- Wands --
	{
		"wand of fire", I_WAND, UNID_WAND,
		W_FIRE, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_UNUSUAL,
		1,
		A_NONE,
	},

	{
		"wand of cold", I_WAND, UNID_WAND,
		W_COLD, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_UNUSUAL,
		1,
		A_NONE,
	},

	{
		"wand of lightning", I_WAND, UNID_WAND,
		W_LIGHTNING, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_RARE,
		1,
		A_NONE,
	},

	{
		"wand of force", I_WAND, UNID_WAND,
		W_FORCE, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"wand of teleportation", I_WAND, UNID_WAND,
		W_TELEPORTATION, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_UNUSUAL,
		1,
		A_NONE,
	},

	{
		"wand of invisibility", I_WAND, UNID_WAND,
		W_INVISIBILITY, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"wand of marking", I_WAND, UNID_WAND,
		W_MARKING, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"wand of sleep", I_WAND, UNID_WAND,
		W_SLEEP, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"wand of charming", I_WAND, UNID_WAND,
		W_CHARMING, 0, 1, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 1,
		F_NORMAL,
		1,
		A_NONE,
	},

	// -- Potions --
	{
		"potion of water", I_POTION, UNID_POT,
		P_WATER, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_PROLIFIC,
		1,
		A_INTENSE | A_CYAN,
	},

	{
		"potion of healing", I_POTION, UNID_POT,
		P_HEALING, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of full healing", I_POTION, UNID_POT,
		P_FULLHEALING, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_UNUSUAL,
		1,
		A_NONE,
	},

	{
		"potion of invisibility", I_POTION, UNID_POT,
		P_INVISIBILITY, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of paralysis", I_POTION, UNID_POT,
		P_PARALYSIS, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of sleeping", I_POTION, UNID_POT,
		P_SLEEPING, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of confusion", I_POTION, UNID_POT,
		P_CONFUSION, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of poison", I_POTION, UNID_POT,
		P_POISON, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of infravision", I_POTION, UNID_POT,
		P_INFRAVISION, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_NORMAL,
		1,
		A_NONE,
	},

	{
		"potion of gain level", I_POTION, UNID_POT,
		P_GAIN_LEVEL, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_NONE, 15,
		F_RARE,
		1,
		A_NONE,
	},

	// -- Books --
	{
		"Book of Life", I_BOOK, UNID_NONE,
		S_NONE, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_BOL, 1,
		F_NEVER,
		1,
		A_RED,
	},

	// -- Tools --
	{
		"key", I_TOOL, UNID_NONE,
		S_NONE, 0, 3, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_KEY, 1,
		F_NORMAL,
		1,
		A_CYAN,
	},

	// -- Misc --
	{
		"silver piece", I_MISC, UNID_NONE,
		S_NONE, 0, 0, I_NONE,
		0, 0,
		{ 0, 0 },
		{ 0, 0 },
		U_SILVER_PIECE, 65535,
		F_PROLIFIC,
		1,
		A_INTENSE | A_WHITE,
	},
};

const char * const ITEMTYPESTRS[] = {
	"Weapon",
	"Armor",
	"Wand",
	"Potion",
	"Scroll",
	"Book",
	"Tool",
	"Misc",
};

item_t BuildItem(uint8_t type, int8_t enchant, uint8_t holiness)
{
	item_t i;

	memset(&i, 0, sizeof(item_t));
	i.type = type;
	i.enchant = enchant;
	i.holiness = holiness;
	i.knownholy = B_UNKNOWN;
	i.ided = 0;
	i.x = 0;
	i.y = 0;
	i.dlvl = 1;
	i.branch = B_DUNGEONS;
	if (type == ID_SILVER_PIECE)
	{
		i.count = plyrmon.dlvl + rn2(plyrmon.dlvl * 100);
	}
	else if (ITEMDEFS[type].maxgenstack > 1)
	{
		i.count = 1 + rn1(ITEMDEFS[type].maxgenstack);
	}
	else
	{
		i.count = 1;
	}

	return i;
}

const char *ItemDesc(const item_t *item, uint8_t invidx)
{
	static char desc[256];
	char add[256];

	strcpy(desc, "");
	if (item->count != 1)
	{
		sprintf(desc, "%i ", item->count);
	}
	switch (item->knownholy)
	{
		case B_UNHOLY:
			strcat(desc, "unholy ");
			break;
		case B_CURSED:
			strcat(desc, "cursed ");
			break;
		case B_HOLY:
			strcat(desc, "holy ");
			break;
	}
	if (item->ided)
	{
		switch (ITEMDEFS[item->type].type)
		{
			case I_WEAPON:
			case I_ARMOR:
				sprintf(add, "%+i ", item->enchant);
				strcat(desc, add);
				break;
		}
	}
	if (invidx != INV_NONE && wielded(invidx))
	{
		strcat(desc, "wielded ");
	}
	if (invidx != INV_NONE && worn(invidx))
	{
		strcat(desc, "worn ");
	}
	if (itypeided[item->type])
	{
		sprintf(add, "%s", ITEMDEFS[item->type].name);
	}
	else
	{
		if (strcmp(itypedesc[item->type], ""))
		{
			sprintf(add, "%s called %s", UnknownItemDesc(item->type), itypedesc[item->type]);
		}
		else
		{
			sprintf(add, "%s", UnknownItemDesc(item->type));
		}
#ifdef _WIZARD
		sprintf(add, "%s [%s]", add, ITEMDEFS[item->type].name);
#endif
	}
	strcat(desc, add);
	if (item->count == 1)
	{
		strcpy(add, desc);
		if (add[0] == 'a' || add[0] == 'e' || add[0] == 'i' || add[0] == 'o' || add[0] == 'u')
		{
			strcpy(desc, "an ");
		}
		else
		{
			strcpy(desc, "a ");
		}
		strcat(desc, add);
	}
	else
	{
		strcat(desc, "s");
	}

	return desc;
}
